package com.workManager.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiOperation;
import com.workManager.base.BaseController;
import com.workManager.exception.BusinessException;
import com.workManager.pojo.TbCostEstimate;
import com.workManager.service.CostEstimateService;
import com.workManager.util.WebUtil;
import com.workManager.vo.CostVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Thinkpad on 2017/1/16 0016.
 */
@Controller
@RequestMapping("/costEstimate")
public class CostEstimateController extends BaseController {

    @Autowired
    private CostEstimateService costEstimateService;


    @ApiOperation(value = "人工成本预估查询", notes = "人工成本预估查询")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public Object query(Integer projectId) {
        Map<String, Object> map = new HashMap<String, Object>();
        String loginCode;
        try {
            loginCode = WebUtil.getCurrentUserVO().getLoginCode();
        } catch (Exception e) {
            throw new BusinessException("未登录");
        }
        map.put("loginCode", loginCode);
        map.put("projectId", projectId);
        ModelMap modelMap = new ModelMap();
        return setSuccessModelMap(modelMap, costEstimateService.query(map));
    }

    @ApiOperation(value = "新增人工成本预估", notes = "新增人工成本预估")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object add(@RequestBody TbCostEstimate costEstimate) {
        costEstimate.setCreateDate(new Date());
        String loginCode;
        try {
            loginCode = WebUtil.getCurrentUserVO().getUserName();
        } catch (Exception e) {
            throw new BusinessException("未登录");
        }
        costEstimate.setOperator(loginCode);
        ModelMap modelMap = new ModelMap();
        costEstimateService.add(costEstimate);
        return setSuccessModelMap(modelMap, "添加成功");
    }

    @ApiOperation(value = "作废", notes = "作废")
    @RequestMapping(value = "/invalid", method = RequestMethod.POST)
    public Object invalid(@RequestBody TbCostEstimate costEstimate) {
        ModelMap modelMap = new ModelMap();
        costEstimateService.invalid(costEstimate);
        return setSuccessModelMap(modelMap, "作废成功");
    }

    @ApiOperation(value = "发生人工部门", notes = "发生人工部门")
    @RequestMapping(value = "/orgList", method = RequestMethod.POST)
    public Object orgList() {
        ModelMap modelMap = new ModelMap();
        List<Map<String,Object>> list = costEstimateService.orgList();
        return setSuccessModelMap(modelMap, list);
    }

    @ApiOperation(value = "列表页面数据", notes = "列表页面数据")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object list(
            @RequestParam(value = "pageNumber",required = false) String pageNumber,
            @RequestParam(value="pageSize", required = false) String pageSize,
            @RequestParam(required = false) String projectCode, @RequestParam(required = false) String projectName, @RequestParam(required = false) Integer orgId) {
        ModelMap modelMap = new ModelMap();
        Map<String,Object> dataMap = new HashMap<String, Object>();
        Map<String,Object> map = new HashMap<>();
        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);

        String loginCode;
        try {
            loginCode = WebUtil.getCurrentUserVO().getLoginCode();
        }catch (Exception e){
            throw new BusinessException("未登录");
        }

        map.put("loginCode", loginCode);
        map.put("pagenum",pagenum);
        map.put("pagesize",pagesize);
        map.put("projectCode",projectCode);
        map.put("projectName",projectName);
        map.put("orgId",orgId);

        PageInfo<CostVo> pageInfo = null;
        try {
            pageInfo = costEstimateService.list(map);
        }catch (Exception e){
            e.printStackTrace();
            String msg = e.getMessage();
            System.out.println(msg);
        }

        if(pageInfo!=null){
            dataMap.put("total",pageInfo.getTotal());
            dataMap.put("rows",pageInfo.getList());
        }

        return setSuccessModelMap(modelMap, dataMap);
    }

    @ApiOperation(value = "图表数据", notes = "图表数据")
    @RequestMapping(value = "/charts", method = RequestMethod.POST)
    public Object charts(@RequestParam Integer projectId, @RequestParam(required = false) Integer orgId) {
        ModelMap modelMap = new ModelMap();
        Map<String,Object> map = new HashMap<>();
        map.put("projectId",projectId);
        map.put("orgId",orgId);
        return setSuccessModelMap(modelMap, costEstimateService.queryByMonth(map));
    }

    @ApiOperation(value = "完成审批", notes = "完成审批")
    @RequestMapping(value = "/approval", method = RequestMethod.POST)
    public Object approval(@RequestBody List<Integer> ids) {
        ModelMap modelMap = new ModelMap();
        costEstimateService.approval(ids);
        return setSuccessModelMap(modelMap, "审批成功");
    }

    @ApiOperation(value = "按部门和项目审批", notes = "按部门和项目审批")
    @RequestMapping(value = "/approvalByOrg", method = RequestMethod.POST)
    public Object approvalByOrg(@RequestParam Integer orgId, @RequestParam Integer projectId) {
        ModelMap modelMap = new ModelMap();
        costEstimateService.approvalByOrg(orgId, projectId);
        return setSuccessModelMap(modelMap, "审批成功");
    }

    @ApiOperation(value = "查询已审批汇总", notes = "查询已审批汇总")
    @RequestMapping(value = "/queryApproved", method = RequestMethod.POST)
    public Object queryApproved(@RequestParam Integer projectId) {
        ModelMap modelMap = new ModelMap();
        Map map = new HashMap();
        map.put("projectId", projectId);
        map.put("statusName", "已审批");
        return setSuccessModelMap(modelMap, costEstimateService.queryGather(map));
    }

    @ApiOperation(value = "查询待审批汇总", notes = "查询待审批汇总")
    @RequestMapping(value = "/queryPendApproval", method = RequestMethod.POST)
    public Object queryPendApproval(@RequestParam Integer projectId) {
        ModelMap modelMap = new ModelMap();
        Map map = new HashMap();
        map.put("projectId", projectId);
        map.put("statusName", "待审批");
        return setSuccessModelMap(modelMap, costEstimateService.queryGather(map));
    }

}
